package com.renwei.yunlong.activity.work;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.fingdo.statelayout.StateLayout;
import com.google.gson.Gson;
import com.renwei.yunlong.R;
import com.renwei.yunlong.adapter.ProjectSelectAdapter;
import com.renwei.yunlong.base.BaseActivity;
import com.renwei.yunlong.bean.SelectBelongsBean;
import com.renwei.yunlong.http.HttpTaskListener;
import com.renwei.yunlong.utils.JsonMapUtil;
import com.renwei.yunlong.utils.ServiceRequestManager;
import com.renwei.yunlong.view.SimpleOptionView;
import com.zyf.fwms.commonlibrary.base.baseadapter.BaseRecyclerViewAdapter;
import com.renwei.yunlong.utils.CommonUtils;
import com.zyf.fwms.commonlibrary.utils.StatusBarUtil;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * 选择所属项目---页面
 * <p>
 * Created by Administrator on 2017/8/31.
 */

public class ProjectSelectActivity extends BaseActivity implements HttpTaskListener, BaseRecyclerViewAdapter.OnItemClickLitener {

    @Bind(R.id.simpleTileView)
    SimpleOptionView simpleTileView;
    @Bind(R.id.recyclerView)
    RecyclerView recyclerView;
    @Bind(R.id.state_layout)
    StateLayout stateLayout;
    private ProjectSelectAdapter adapter;
    private String ownerCompanyCode;
    private String contractId;
    private String hardwareIds;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_belongs);
        ButterKnife.bind(this);
        StatusBarUtil.setColor(this, CommonUtils.getColor(this, R.color.title_color), 0);

        ownerCompanyCode = getIntent().getStringExtra("ownerCompanyCode");
        contractId = getIntent().getStringExtra("contractId");
        hardwareIds = getIntent().getStringExtra("hardwareIds");

        simpleTileView.setTitle("选择所属项目");
        simpleTileView.setRightText("清除", new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra("contractName", "");
                intent.putExtra("contractId","");
                setResult(RESULT_OK, intent);
                ProjectSelectActivity.this.finish();
            }
        });
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new ProjectSelectAdapter(ProjectSelectActivity.this, contractId);
        adapter.setOnItemClickLitener(this);
        recyclerView.setAdapter(adapter);

        initData();
    }

    private void initData() {
        HashMap<String, String> map = new HashMap<>();
        map.put("currentUserId", serviceLoginBean.getRows().getEmployeeId());
        map.put("serviceProviderCode", serviceLoginBean.getRows().getServiceProviderCode());
        map.put("ownerCode", ownerCompanyCode);
        map.put("hardwareId",hardwareIds);
        ServiceRequestManager.getManager().getWorkProject(this, JsonMapUtil.mapToJson(map), this);
    }

    @Override
    public void onItemClick(View view, int position) {
        SelectBelongsBean.Rows bean = adapter.getItem(position);
        Intent intent = new Intent();
        intent.putExtra("contractName", bean.getContractName());
        intent.putExtra("contractId", String.valueOf(bean.getContractId()));
        intent.putExtra("ownerCode", String.valueOf(bean.getOwnerCode()));
        intent.putExtra("serverName", String.valueOf(bean.getServerName()));
        intent.putExtra("bindFlag", String.valueOf(bean.getBindFlag()));
        setResult(RESULT_OK, intent);
        ProjectSelectActivity.this.finish();
    }

    @Override
    public void onSuccess(int requestId, String result) {
        SelectBelongsBean bean = new Gson().fromJson(result, SelectBelongsBean.class);
        if (bean.getMessage().getCode() == 200) {
            adapter.setData(bean.getRows());

            if (bean.getRows() != null && bean.getRows().size() != 0) {
                stateLayout.showContentView();
            } else {
                stateLayout.showEmptyView();
            }
        }
    }

    @Override    public void onException(int requestId, String code) {        showTopWrongMsg(getResources().getString(R.string.net_work_error));    }

    public static void OpenActivity(Object context, String ownerCompanyCode, String contractId, String hardwareIds, int requestCode) {
        if (context instanceof Context) {
            Intent intent = new Intent((Context) context, ProjectSelectActivity.class);
            intent.putExtra("ownerCompanyCode", ownerCompanyCode);
            intent.putExtra("contractId", contractId);
            intent.putExtra("hardwareIds", hardwareIds);
            ((Activity) context).startActivityForResult(intent, requestCode);
        } else if (context instanceof Fragment) {
            Intent intent = new Intent(((Fragment) context).getContext(), ProjectSelectActivity.class);
            intent.putExtra("ownerCompanyCode", ownerCompanyCode);
            intent.putExtra("contractId", contractId);
            intent.putExtra("hardwareIds", hardwareIds);
            ((Fragment) context).startActivityForResult(intent, requestCode);
        }
    }

}
